﻿using System;
using System.Collections.Generic;
using System.Linq;
using AndyPike.Unfuddle.Commands.Projects;
using AndyPike.Unfuddle.Entities.Projects;
using AndyPike.Unfuddle.Exceptions;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace AndyPike.Unfuddle.Tests.Integration
{
    //[Ignore("Just ignoring integration tests for now to test unit test coverage")]
    public class When_using_the_unfuddle_service_with_projects : IntegrationTestBase
    {
        [Test]
        public void Should_retrieve_a_list_of_projects()
        {
            List<Project> projects = unfuddleService.Execute(new FindAllProjects());

            Assert.That(projects, Is.Not.Null);
            Assert.That(projects.Count, Is.GreaterThan(0));
        }

        [Test]
        public void Should_retrieve_a_single_project_given_its_id()
        {
            Project project = unfuddleService.Execute(new FindProjectById(projectId));

            Assert.That(project, Is.Not.Null);
            Assert.That(project.Id, Is.EqualTo(projectId));
        }

        [Test]
        [ExpectedException(typeof(NotFoundRequestException))]
        public void Should_throw_an_exception_if_the_project_id_is_invalid()
        {
            unfuddleService.Execute(new FindProjectById(-1));
        }

        [Test]
        public void Should_update_a_project()
        {
            Guid testId = Guid.NewGuid();
            
            //Load a project from the Unfuddle server and add a Guid to the description
            Project project = unfuddleService.Execute(new FindProjectById(projectId));
            project.Description += " -" + testId;

            //Save the project
            unfuddleService.Execute(new UpdateProject(project));

            //Load the project again and check that the Guid is present
            Project savedProject = unfuddleService.Execute(new FindProjectById(projectId));

            Assert.That(savedProject, Is.Not.Null);
            Assert.That(savedProject.Id, Is.EqualTo(projectId));
            Assert.That(savedProject.Description.Contains(testId.ToString()), Is.True);
        }

        [Test]
        public void Should_create_a_new_project()
        {         
            List<Project> projectsBefore = unfuddleService.Execute(new FindAllProjects());

            var project = new Project
            {
                Title = "Create Project Test",
                Description = "This is a description",
                Theme = ProjectThemes.Green,
                AssigneeOnResolve = ProjectResolveActions.None,
                Archived = false,
                EnableTimeTracking = false,
                CloseTicketSimultaneouslyDefault = false,
                ShortName = "create-test"
            };

            unfuddleService.Execute(new CreateProject(project));
 
            List<Project> projectsAfter = unfuddleService.Execute(new FindAllProjects());

            Assert.That(projectsAfter.Count, Is.GreaterThan(projectsBefore.Count));
        }

        [Test]
        public void Should_delete_an_existing_project()
        {
            var project = new Project
            {
                Title = "Delete Project Test",
                Description = "This is a description",
                Theme = ProjectThemes.Green,
                AssigneeOnResolve = ProjectResolveActions.None,
                Archived = false,
                EnableTimeTracking = false,
                CloseTicketSimultaneouslyDefault = false,
                ShortName = "delete-test"
            };

            unfuddleService.Execute(new CreateProject(project));

            List<Project> projectsBefore = unfuddleService.Execute(new FindAllProjects());
            Project projectToDelete = projectsBefore.Where(p => p.ShortName == "delete-test").Single();

            unfuddleService.Execute(new DeleteProject(projectToDelete.Id));

            List<Project> projectsAfter = unfuddleService.Execute(new FindAllProjects());

            Assert.That(projectsAfter.Count, Is.EqualTo(projectsBefore.Count - 1));
        }
    }
}